﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using DAO.WaiGaoQiao;

namespace WaiGaoQiaoWeb.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
        private Con_GoodsInfo conInfo = new Con_GoodsInfo(true);
        private SupplierInfo supInfo = new SupplierInfo(true);
        private Work_ArticleInfo workInfo = new Work_ArticleInfo();
        private Work_post wp = new Work_post();
        public ActionResult Index()
        {
            return RedirectToAction("RtnSpoGoodsList");
        }
        /// <summary>
        /// 获取所有主副调食材
        /// </summary>
        /// <returns></returns>
        public ActionResult RtnSpoGoodsList(string GoodsName)
        {
            string temp = "and PriorID not in(select ID from Con_Goods AS Con_Goods_1 where GoodsName='零星物品')";
            int groupCount = conInfo.GetCon_GoodsCount(temp);
            IEnumerable<Con_GoodsInfo> list = conInfo.GetAllCon_Goods(temp).Where(x => x.GoodsName.ToLower().StartsWith(GoodsName));
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 返回物资名称列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult RtnWorkArticleList(string ArticleName)
        {
            string temp = " and ArticleName like '%" + ArticleName + "%'";
            IEnumerable<Work_ArticleInfo> list = workInfo.GetAllCon_Article(temp);
            return Json(list, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 返回工种岗位名列表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult RtnPostNameList(string StructrueName)
        {
            string temp = " AND [structrueName] LIKE '%" + StructrueName + "%'";
            IEnumerable<Work_post> list = wp.GetAllWork_Post(temp);
            return Json(list, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 返回物资名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult RtnWorkArticleName(int id)
        {
            return Json(DataAccessWork_Article.GetArticleNameByID(id));
        }
        /// <summary>
        /// 获取所有食材
        /// </summary>
        /// <param name="GoodsName"></param>
        /// <returns></returns>
        public ActionResult RtnSpoAllGoodsList(string GoodsName)
        {
            int groupCount = conInfo.GetCon_GoodsCount("");
            IEnumerable<Con_GoodsInfo> list = conInfo.GetAllCon_Goods("", groupCount, 1).Where(x => x.GoodsName.ToLower().StartsWith(GoodsName));
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取所有仓库中有的主副调食材
        /// </summary>
        /// <param name="GoodsName"></param>
        /// <returns></returns>
        public ActionResult RtnSpoGoodsStocksList(string GoodsName)
        {
            string temp = GoodsName;
            IList<Con_GoodsInfo> list = conInfo.GetCon_Goods_StocksList(temp);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 从库存表中获取主副调食材的库存
        /// </summary>
        /// <param name="GoodsName"></param>
        /// <returns></returns>
        public ActionResult RtnSpoGoodsStocksList1(string GoodsName)
        {
            string temp = GoodsName;
            IList<Con_GoodsInfo> list = conInfo.GetCon_Goods_StocksList1(temp);
            return Json(list, JsonRequestBehavior.AllowGet);
        } 

        /// <summary>
        /// 获取所有仓库中有的零星物品食材
        /// </summary>
        /// <param name="GoodsName"></param>
        /// <returns></returns>
        public ActionResult RtnSpoGoodsStocksLXList(string GoodsName)
        {
            //string temp = "and GOODSNO in( select GoodsNo from Con_Goods where PriorID in(select ID from Con_Goods  where GoodsName='零星物品'))";
            //IEnumerable<Con_GoodsInfo> list = conInfo.GetCon_Goods_Stocks_SporadicList(temp).Where(x => x.GoodsName.ToLower().StartsWith(GoodsName));
            //return Json(list, JsonRequestBehavior.AllowGet);
            string temp = GoodsName;
            IList<Con_GoodsInfo> list = conInfo.GetCon_Goods_Stocks_SporadicList(temp);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取零星物品食材
        /// </summary>
        /// <returns></returns>
        public ActionResult RtnSpotsficList(string GoodsName)
        {
            string temp = "AND (PriorID IN (SELECT ID FROM Con_Goods AS Con_Goods_1 WHERE (GoodsName = '零星物品')))";
            int groupCount = conInfo.GetCon_GoodsCount(temp);
            IEnumerable<Con_GoodsInfo> list = conInfo.GetAllCon_Goods(temp).Where(x => x.GoodsName.ToLower().StartsWith(GoodsName));
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <param name="Supplier_Name"></param>
        /// <returns></returns>
        public ActionResult RtnSupplierList(String Supplier_Name)
        {
            int supCount = supInfo.GetSupplierCount();
            IEnumerable<SupplierInfo> list = supInfo.SelSupplierInfo(supCount, 1, "").Where(m => m.Supplier_Name.ToLower().StartsWith(Supplier_Name));
            return Json(list, JsonRequestBehavior.AllowGet);
        }
   
        /// <summary>
        /// 获得商品名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult RtnGoodsName(int Id)
        {
            return Json(Con_GoodsInfo.GetGoodsNameByID(Id));
        }
        /// <summary>
        /// 获得供应商名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult RtnSupplierName(int Id)
        {
            return Json(SupplierInfo.GetSupplierName(Id));
        }
        /// <summary>
        ///判断食材是否存在 
        /// </summary>
        /// <param name="Con_GoodsInfoGoodsName"></param>
        /// <returns></returns>
        public ActionResult IsExist(string Con_GoodsInfoGoodsName)
        {
            string conditon = "AND GoodsName='" + Con_GoodsInfoGoodsName + "'";
            int rows = conInfo.GetCon_GoodsCount(conditon);
            return Json(rows > 0 ? true : false);
        }
        /// <summary>
        /// 获取出库的食材名称
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOutGoodsName(string GoodsName)
        {
            Dictionary<int, string> dic = DataAccessCon_Goods.GetOutGoodsName(GoodsName);
            Con_GoodsInfo goods = null;
            IList<Con_GoodsInfo> en = new List<Con_GoodsInfo>();
            foreach (KeyValuePair<int, string> item in dic)
            {
                goods = new Con_GoodsInfo();
                goods.Id = item.Key;
                goods.GoodsName = item.Value;
                en.Add(goods);
            }

            return Json(en, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 根据食材id来获取所有的价格列表
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public ActionResult GetPriceListByGoodsId(int? goodsId)
        {
            List<double> d = DataAccessCon_Goods.GetPriceListByGoodsId(goodsId);
            SelectListItem item = null;
            IList<SelectListItem> en = new List<SelectListItem>();
            for (int i = 0; i < d.Count; i++)
            {
                item = new SelectListItem();
                item.Text = d[i].ToString();
                item.Value = d[i].ToString();
                en.Add(item);
            }
            return Json(en);
        }

    }
}
